//JS容错代码，不提示错误。
window.onerror=function (){return true;}
$(document).ready(function (){init();})
function init(){
	var activeid = $("#activeid").val();
	if(activeid!=0){
	  	var starTime = $("#ac_start_time_"+activeid).val();
	   	var starTimeLong = $("#ac_start_time_long_"+activeid).val();
	   	var endTime = $("#ac_end_time_"+activeid).val();
	   	var endTimeLong = $("#ac_end_time_long_"+activeid).val();
		var t = starTime.split(" ");
		$("#startime_str").val(t[0]);
		var ms = t[1].split(":");
		$("#startime_hour").val(ms[0]);
		$("#startime_min").val(ms[1]);
		$("#endtime").html(endTime);
		$("#endtime").parent().attr("style","display:block");
		var sc = (endTimeLong-starTimeLong)/1000/60;
		$("#active_time").val(sc);
	}
}
var PhotoLocalName = "";
//online_week
function centerChange(){
	//if(online_week>=20){
	  if($("#center_flag").attr("checked")=="checked"){
	      $("#tag_category").removeAttr("disabled");
		  $("#tag_id").removeAttr("disabled");
	  }else{
	      $("#tag_category").attr("disabled","disabled");
		  $("#tag_id").attr("disabled","disabled");
	  }
  //}
}

var activetagstr="";
function activeTag(tagid)
{
   if (activetagstr==""){ 
    var request = $.ajax(
     {
        url : "/room!getActiveTag.jspa", type : "post", async:false,
        dataType : "json", complete : function (data, textStatus) {
		    activetagstr=data.responseText;
		}
      }
	 );
	}
	  var re=eval("(" + activetagstr + ")");
      if (!re.hasOwnProperty("result")){
		      if (tagid==-1){
			     for(var i=0;i<re.tags.length;i++){
				    $("#tag_category").append("<option value='"+re.tags[i].id+"'>"+re.tags[i].tagName+"</option>");
				 }
				}else{
				   for(var i=0;i<re.tags.length;i++){
				     if(tagid==0){
					    $("#tag_id option").remove();
					    $("#tag_id").append("<option value='0'>\u8BF7\u9009\u62E9</option>");
						return;
					 }
				     if (re.tags[i].id==tagid){
					    $("#tag_id option").remove();
					    var ctag=re.tags[i];
						for(var i=0;i<ctag.tags.length;i++){
				           $("#tag_id").append("<option value='"+ctag.tags[i].id+"'>"+ctag.tags[i].tagName+"</option>");
				        }
						break;
					 }
				 }
				}
		  }
    
}
function activeReIndex()
{
  if($('#active_avatar').attr('src').indexOf("?")>0){
     delImg($('#active_avatar').attr('src'));
  }
  window.location.href="/room!index.jspa?roomid="+$('#room_id').val();
  
}

//Add
function subActive()
{
	var activeId = $("#activeid").val();
	var room_id = $('#room_id').val();
	if(activeId!=0){
		subEditActive(activeId,room_id);return;
	}
    if ($('#active_subject').val() == "\u6D3B\u52A8\u6807\u9898\u6700\u591A\u8F93\u516515\u4E2A\u6C49\u5B57") {
        alert("\u6D3B\u52A8\u6807\u9898\u4E0D\u80FD\u4E3A\u7A7A");
        return;
    }
    if(getlengthStr($('#active_subject').val())>30){
    	alert("\u6807\u9898\u4e0d\u80fd\u8d85\u8fc715\u4e2a\u6c49\u5b57");
    	return;
    }
    
    if ($('#startime_str').val() == "\u5F00\u59CB\u65E5\u671F" ||$('#startime_hour').val()=="" ||$('#startime_hour').val()=="") {
        alert("\u6D3B\u52A8\u5F00\u59CB\u65F6\u95F4\u4E0D\u80FD\u4E3A\u7A7A");
        return;
    }
    if ($("#active_time").val() ==0) {
        alert("\u8bf7\u9009\u62e9\u6d3b\u52a8\u65f6\u957f");
        return;
    }    
	if ($('#active_content').val()=="\u6D3B\u52A8\u4ECB\u7ECD\u6700\u591A50\u4E2A\u6C49\u5B57") {
        $('#active_content').val("");
    }else{
		if (getlengthStr($('#active_content').val()) > 200) {
			alert("\u6D3B\u52A8\u7B80\u4ECB\u8981\u6C42\u5728200\u5B57\u4EE5\u5185");
			return;
		}
	}
	var center_flag=0;
	if($("#center_flag").attr("checked")=="checked"){
	    center_flag=1;
	}
    var request = $.ajax(
    {
        url : "/room!addActive.jspa", type : "post", data : 
        {
            "active.active_subject" : $('#active_subject').val(), "active.startime_str" : $('#startime_str').val()+" "+$('#startime_hour').val()+":"+$('#startime_min').val(), 
            "active.endtime_str" : $('#endtime').html(), "active.active_content" : $('#active_content').val(), 
			"active.tag_catalog_id" : $('#tag_category').val(),"active.tag_id" : $('#tag_id').val(),"active.center_flag" : center_flag,
            "active.room_id" : room_id, "photo.photo_path" : $('#active_avatar').attr("src"), 
            "photo.photo_type_id" : 3, "photo.size_id" : 3, "photo.photo_name" : PhotoLocalName,
            "fileURL":$("#uf_pho_path").val(),"filename":$("#uf_pho_name").html()
            //+"?f="+(new Date()).getMilliseconds()
        },
        dataType : "json",
        complete : function (data, textStatus)
        {
        	if(data.responseText==""){
        		alert("\u6dfb\u52a0\u6d3b\u52a8\u53d1\u751f\u9519\u8bef\u3002");
        		return;
        	}
        	var result = eval("(" + data.responseText + ")").result;
            if ( result== 0)
            {
                $('#active_subject').val("");
                $('#startime_str').val("");
				$('#startime_hour').val("00");
				$('#startime_min').val("00");
                $('#endtime').html("");
                $('#active_content').val("");
                $("#active_time").val("0");
                $('#active_avatar').attr("src", "/images/Head.jpg");
				$("#ywc").hide();
				$('#uploadactive').show();
				$("#center_flag").attr("checked",false);
				$('#tag_category').val("0");
				$("#tag_id option").remove();
				$("#tag_id").append("<option value='0'>\u8BF7\u9009\u62E9</option>");
				$('#tag_id').val("0");
				$("#tag_category").attr("disabled","disabled");
			    $("#tag_id").attr("disabled","disabled");
                alert("\u6210\u529F\u6DFB\u52A0\u6D3B\u52A8");
                window.location.href="/room!index.jspa?roomid="+room_id;
            }
            else if(result==-1){//没有登录
            	alert("\u8bf7\u767b\u5f55\u540e\u518d\u6dfb\u52a0\u6d3b\u52a8\u3002");
            	return ;
            }
            else if(result==-2){//不能重叠
            	alert("\u8be5\u6d3b\u52a8\u4e0e\u5176\u4ed6\u6d3b\u52a8\u65f6\u95f4\u6709\u91cd\u53e0\uff0c\u8bf7\u91cd\u65b0\u9009\u62e9\u8d77\u59cb\u65f6\u95f4\u3002");
            	return ;
            }
            else if(result==-3){//活动开始时间已过期。
            	alert("\u6d3b\u52a8\u5f00\u59cb\u65f6\u95f4\u5df2\u8fc7\u671f\u3002");
            	return ;
            }
            else {
                alert("\u6DFB\u52A0\u6D3B\u52A8\u5931\u8D25");
            }
        }
    });
}



function subEditActive(activeId,room_id)
{
    if ($('#active_subject').val() == "") {
        alert("\u6D3B\u52A8\u6807\u9898\u4E0D\u80FD\u4E3A\u7A7A");
        return;
    }
   	var fileURL =$("#uf_fileURL").val();
   	var filename = $("#uf_filename").val();
   	var tfileURL =  $("#uf_pho_path").val();
   	if(tfileURL!=''){
   		fileURL = tfileURL;
   		filename = $("#uf_pho_name").html();
   	}
    var request = $.ajax(
    {
        url : "/room!editActive.jspa", type : "post", data : 
        {
            "active.active_subject" : $('#active_subject').val(),"active.startime_str" : $('#startime_str').val()+" "+$('#startime_hour').val()+":"+$('#startime_min').val(), 
            "active.endtime_str" : $('#endtime').html(),"active.active_content" : $('#active_content').val(), "active.room_id" : $('#room_id').val(),
            "active.id" : activeId,"active.image" :$('#ac_photoid_'+activeId).val(), "photo.photo_path" : $('#active_avatar').attr("src"), 
            "photo.photo_type_id" : 3, "photo.size_id" : 3, "photo.photo_name" : PhotoLocalName,
            "active.attachment":$("#uf_fileid").val(),"fileURL":fileURL,"filename":filename,"fileid":$("#uf_fileid").val()
        },
        dataType : "json",
        complete : function (data, textStatus)
        {
        	var result = eval("(" + data.responseText + ")").result;
            if (result == 0)
            {
                $('#active_subject').val("");
                $('#startime_str').val("");
                $('#endtime').html("");
                $('#active_content').val("");
                $("#active_time").val("0");
                $('#active_avatar').attr("src", "/images/Head.jpg");
				$("#ywc").hide();
				$('#uploadactive').show();
				$("#activeSub").attr("href", "javascript:subActive();");
				if($("#preactiveAvatar" + activeId).attr("src")!=null&&$("#preactiveAvatar" + activeId).attr("src")!=""&&$('#ac_photoid_'+activeId).val()==0){
				  delImg($("#preactiveAvatar" + activeId).attr("src"));
				 }
				 $("#startime_str").removeAttr("disabled");
				 $("#startime_hour").removeAttr("disabled");
				 $("#startime_min").removeAttr("disabled");
				 $("#active_time").removeAttr("disabled");
				 $('#startime_hour').val("00");
				$('#startime_min').val("00");
                alert("\u6210\u529F\u4FEE\u6539\u6D3B\u52A8");
                window.location.href="/room!index.jspa?roomid="+room_id;
            }else if(result==-1){//没有登录
            	alert("\u8bf7\u767b\u5f55\u540e\u518d\u6dfb\u52a0\u6d3b\u52a8\u3002");
            	return ;
            }
            else if(result==-2){//不能重叠
            	alert("\u8be5\u6d3b\u52a8\u4e0e\u5176\u4ed6\u6d3b\u52a8\u65f6\u95f4\u6709\u91cd\u53e0\uff0c\u8bf7\u91cd\u65b0\u9009\u62e9\u8d77\u59cb\u65f6\u95f4\u3002");
            	return ;
            }
            else if(result==-3){//活动开始时间已过期。
            	alert("\u6d3b\u52a8\u5f00\u59cb\u65f6\u95f4\u5df2\u8fc7\u671f\u3002");
            	return ;
            }
            else {
                alert("\u4FEE\u6539\u6D3B\u52A8\u5931\u8D25");
            }
        }
    });
}
function delImg(srcImg)
{
    var index = srcImg.indexOf("?");
    if (index > 0) {
        srcImg = srcImg.substring(0, index);
    }
    var request = $.ajax(
    {
        url : "/room!delImg.jspa", type : "post", data : {
            imgsrc : srcImg
        },
        dataType : "json", complete : function (data, textStatus) { }
    });
     if(imgType==4){
   		$("#uploadFileprocess").hide();
	}else{
    	$("#uploadImgprocess").hide();
	}
}
